package com.lanou.controller;

import com.lanou.bean.*;
import com.lanou.mapper.logindao.LoginMapper;
import com.lanou.service.SelectArticalService.SelectArticalServiceImpl;
import com.lanou.service.addatrical.AddArticalServiceImpl;
import com.lanou.service.login.LoginServiceImpl;
import com.lanou.service.register.RegisterServiceImp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by dllo on 18/2/26.
 */
@Controller
public class MainController {

    @Resource
    RegisterServiceImp registerServiceImp;
    @Resource
    LoginServiceImpl loginService;
    @Resource
    AddArticalServiceImpl addArticalService;
    @Resource
    SelectArticalServiceImpl selectArticalService;
    @Resource
    LoginMapper loginMapper;


    @RequestMapping(value = "")
    public String index() {
        return "index";
    }

    @RequestMapping(value = "login")
    public String tologin() {
        return "login";
    }

    @RequestMapping(value = "register")
    public String toregister() {
        return "register";
    }

    //注册
    @ResponseBody
    @RequestMapping(value = "json/register", method = RequestMethod.POST)
    public RegisterResultBean register(@RequestParam("name") String name,
                                       @RequestParam("password") String password,
                                       @RequestParam("tel") String tel,
                                       @RequestParam("address") String address) {
        UserBean bean = registerServiceImp.hasRegister(name);
        if (bean != null) {
            RegisterResultBean resultBean = new RegisterResultBean();
            resultBean.setRegisterResult(false);
            resultBean.setStatue("用户名已经存在,注册失败");
            return resultBean;
        }
        registerServiceImp.registeruser(name, password, tel, address);
        RegisterResultBean resultBean = new RegisterResultBean();
        resultBean.setRegisterResult(true);
        resultBean.setStatue("注册成功");
        return resultBean;
    }

    //登录
    @ResponseBody
    @RequestMapping(value = "json/login", method = RequestMethod.POST)
    public LoginResultBean login(HttpServletResponse response, @RequestParam("name") String name, @RequestParam("password") String password) {
        LoginResultBean login = loginService.login(name, password);


        return login;
    }

    //添加文章
    @ResponseBody
    @RequestMapping(value = "json/addArtical", method = RequestMethod.POST)
    public AddArticalResult addArtical(@RequestParam("title") String title,
                                       @RequestParam("des") String des,
                                       @RequestParam("content") String content,
                                       @RequestParam("userId") int userId) {
        return addArticalService.addAtrical(title, des, content, userId);
    }

    //添加文章跳页
    @RequestMapping(value = "writeblog")
    public String writeblog () {
        return "writeblog";
    }

    //查询文章
    @ResponseBody
    @RequestMapping(value = "json/selectArticalList", method = RequestMethod.GET)
    public ArticalListBean selectArticalListByPage(@RequestParam("page") int page,
                                                   @RequestParam("num") int num,
                                                   @RequestParam(value = "userId" , required = false) Integer userId) {
        if (userId == null){
            return selectArticalService.selectArticalListbypage(page, num);
        }else {
            return selectArticalService.selectArticalListbypageandUserId(page, num,userId);
        }
    }
}
